﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class PressMachineGroup : MonoBehaviour {

    public float upHeight;
    public float downDelay = 3f;
    public float downDuration = 0.8f;
    public float downInterval = 0.5f;

    public float downHeight;
    public float upDelay = 1f;
    public float upDuration = 0.8f;

    public List<Transform> pressMachines = new List<Transform>();

    private void Start() {

        OnCompleteUp();
    }

    void OnCompleteDown() {
        int i = 0;
        int len = pressMachines.Count;
        for (; i < len - 1; i++) {
            Transform machine = pressMachines[i];
            machine.localPosition = new Vector3(machine.localPosition.x, downHeight, 0);
            machine.DOLocalMoveY(upHeight, upDuration)
                   .SetDelay(upDelay).PlayForward();
        }

        Transform last = pressMachines[len - 1];
        last.localPosition = new Vector3(last.localPosition.x, downHeight, 0);
        last.DOLocalMoveY(upHeight, upDuration)
            .SetDelay(upDelay)
            .OnComplete(OnCompleteUp)
            .PlayForward();
    }


    void OnCompleteUp() {
        int i = 0;
        int len = pressMachines.Count;
        for (; i < len - 1; i++) {
            Transform machine = pressMachines[i];
            machine.localPosition = new Vector3(machine.localPosition.x, upHeight, 0);
            machine.DOLocalMoveY(downHeight, downDuration)
                   .SetDelay(i * downInterval + downDelay).PlayForward();
        }

        Transform last = pressMachines[len - 1];
        last.localPosition = new Vector3(last.localPosition.x, upHeight, 0);
        last.DOLocalMoveY(downHeight, downDuration)
            .SetDelay((len - 1) * downInterval + downDelay)
            .OnComplete(OnCompleteDown)
            .PlayForward();
    }


    [ContextMenu("倒序")]
    public void ReverseMachines() {

        pressMachines.Reverse();

    }

}
